package com.bai.ruiji.Controller;

import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.CategoryServer;
import com.bai.ruiji.Service.SetmealDishService;
import com.bai.ruiji.Service.SetmealService;
import com.bai.ruiji.pojo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 27471
 * Time : 2022/10/13 14:12
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryServer categoryServer;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        setmealLambdaQueryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, setmealLambdaQueryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝(把item里的数据拷贝到setmealdto里)
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id，查询分类对象
            Category byId = categoryServer.getById(categoryId);
            if (byId != null) {
                //分类名称
                String name1 = byId.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByid(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除完成");
    }

    /**
     * 停售
     */
    @PostMapping("/status/0")
    public R<String> update(@RequestParam List<Long> ids) {
        UpdateWrapper<Setmeal> queryWrapper = new UpdateWrapper<>();
        queryWrapper.in("id", ids).set("status", 0);
        setmealService.update(queryWrapper);
        return R.success("停售成功");
    }

    /**
     * 启售
     */
    @PostMapping("/status/1")
    public R<String> update1(@RequestParam List<Long> ids) {
        UpdateWrapper<Setmeal> queryWrapper = new UpdateWrapper<>();
        queryWrapper.in("id", ids).set("status", 1);
        setmealService.update(queryWrapper);
        return R.success("启售成功");
    }

    /**
     * 修改
     */
    @PutMapping()
    public R<String> updateDish(@RequestBody SetmealDto setmealDto) {
        setmealService.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes = dishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
        return R.success("修改完成");
    }

    /**
     * 查找前回显
     */
    @GetMapping("/{ids}")
    public R<SetmealDto> save(@PathVariable Long ids) {
        Setmeal byId = setmealService.getById(ids);
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(byId, setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, ids);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    /**
     * 显示套餐内容
     */
    @GetMapping("/list")
    public R<List<Setmeal>> save(Setmeal setmeal) {
        log.info(setmeal.toString());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加口味
     */
   /* @PostMapping("/add")
    public R<>*/
}
